#include "Queue.h"


Queue_t* creat_queue(void)
{
    Queue_t* q = (Queue_t*)calloc(1, sizeof(Queue_t));
    if(q == NULL)
        error(1, errno, "calloc");

    printf("creat_queue\n");
    return q;
}

void destroy_queue(Queue_t* queue)
{
    free(queue);
}

int push_queue(Queue_t* queue, sockfd_t sock_fd)
{

    if(queue->size == MAX_SIZE)
    {
        printf("队列已满\n");
        return -1;
    }
    
    node_t* node = (node_t*)malloc(sizeof(node_t));
    if(node == NULL)
        error(1, errno, "malloc");

    node->val = sock_fd;
    node->next = NULL;

    if(queue->tail != NULL)
    {
        queue->tail->next = node;
        queue->tail = node;
        queue->size++;
    }
    else
    {
        queue->tail = node;
        queue->head = node;
        queue->size++;
    }

    return 0;
}

sockfd_t pop_queue(Queue_t* queue)
{
    if(queue->size == 0)
    {
        printf("队列已空\n");
        return -1;
    }

    node_t* p = queue->head;
    sockfd_t sock_fd = p->val; 
    if(queue->size == 1)
    {
        queue->head = NULL;
        queue->tail = NULL;
    }
    else
        queue->head = queue->head->next;
    
    queue->size--;

    free(p);
    return sock_fd;
}
